package fr.generali.accueilclient.gwt.showcase.client.form.subforms;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class PhoneForm extends Composite implements HasWidgets {

    private static PhoneFormUiBinder uiBinder = GWT.create(PhoneFormUiBinder.class);

    interface PhoneFormUiBinder extends UiBinder<Widget, PhoneForm> {
    }

    @UiField
    SubFormResource res;
    
    @UiField
    FlowPanel rowContainer;
    
    @UiField
    Label phoneTypeHeader;
    
    List<PhoneFormItem> phoneFormItems = new ArrayList<PhoneFormItem>();
    
    public PhoneForm() {
        initWidget(uiBinder.createAndBindUi(this));
        res.style().ensureInjected();
    }

    public void setInError(boolean inError, int index) {
        if (inError) {
            phoneTypeHeader.addStyleName(res.style().headerLabelInError());
        } else {
            phoneTypeHeader.removeStyleName(res.style().headerLabelInError());
        }
        if(phoneFormItems.size() > index){
            phoneFormItems.get(index).setInError(inError);
        }
    }

    @Override
    public void add(Widget w) {
        rowContainer.add(w);
        phoneFormItems.add((PhoneFormItem ) w);
    }

    @Override
    public void clear() {
        rowContainer.clear();
        phoneFormItems.clear();
    }

    @Override
    public Iterator<Widget> iterator() {
        return rowContainer.iterator();
    }

    @Override
    public boolean remove(Widget w) {
        phoneFormItems.remove(w);
        return rowContainer.remove(w);
    }

}
